package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import model.Candidato;
import utilities.Constants;
import utilities.ImageRenderer;
import utilities.Utils;
import controller.ResultadosController;
import controller.StartController;

public class ResultadosView extends JFrame implements GenericView{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8168503252903755527L;
	private JPanel contentPane;
	private ResultadosController controller;
	private static ResultadosView instance = null;
	private DefaultTableModel tableAttributes;
	private JTable table;


	/**
	 * Create the frame.
	 */
	public ResultadosView() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(Constants.SRC_X, Constants.SRC_Y, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
		addWindowListener(new WindowListener() {
			@Override
			public void windowActivated(WindowEvent arg0) {
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				controller.handleButtonCancelar();
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
        });
		setResizable(Boolean.FALSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblConfiguracin = new JLabel("Resultados");
		lblConfiguracin.setBounds(330, 33, 226, 15);
		contentPane.add(lblConfiguracin);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(28, 60, 646, 449);
		contentPane.add(scrollPane);
		tableAttributes = new DefaultTableModel() {
			 /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int mColIndex) {
			        return false;
			    }
		};
		tableAttributes.addColumn ("Nombre");
		tableAttributes.addColumn ("Foto");
		tableAttributes.addColumn("Cant Votos");
		table = new JTable(tableAttributes);
		table.setFocusable(false);
		table.setRowSelectionAllowed(false);
		table.getColumn("Nombre").setPreferredWidth(200);
        table.setRowHeight(Constants.TABLE_HEIGHT);
		table.getColumn("Foto").setPreferredWidth(Constants.TABLE_WIDTH);
		table.getColumn("Cant Votos").setPreferredWidth(5);
		scrollPane.setViewportView(table);
		
		JButton btnCancelar = new JButton("Volver");
		btnCancelar.setBounds(307, 533, 117, 25);
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.handleButtonCancelar();
			}
		});
		contentPane.add(btnCancelar);
	}
	
	public static ResultadosView getInstance() {
		if (instance == null){
			instance = new ResultadosView();
		}
		return instance;
	}

	@Override
	public void showWindow() {
		setVisible(true);
	}

	@Override
	public void hideWindow() {
		setVisible(false);
	}

	public void setController(ResultadosController resultadosController) {
		this.controller = resultadosController;
	}

	public void setList() {
		ArrayList<Candidato> candidates = (ArrayList<Candidato>) ((StartController)this.controller.getParentController()).getManager().getCandidates();
		DefaultTableModel dtm = (DefaultTableModel) this.table.getModel();
		dtm.getDataVector().removeAllElements();
		dtm.fireTableDataChanged();
		table.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer());
		for (Candidato candidato : candidates) {
			Icon icon = Utils.setImageToLabel(Constants.defaultPath + candidato.getId(), Constants.TABLE_WIDTH, Constants.TABLE_HEIGHT);
			Object[] row = {
					candidato.getName(), icon, candidato.getCantVotos()
			};
			dtm.addRow(row);
		}
	}
	
}
